<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorate="~{admin/layout}">

<head>
	<title>Course Manage</title>
	<style>

	</style>
</head>

<body layout:fragment="content">
	<div class="card shadow">
		<div class="card-header border-bottom d-flex justify-content-between align-items-center">
			<h5 class="card-title mb-0">Course List</h5>
			<div class="d-grid gap-2 d-md-block">
				<button class="btn btn-sm btn-primary" type="button" onclick="modifyCourseModal('')">New</button>
			</div>
		</div>
		<div class="card-body">
			<table id="courseTable"></table>
		</div>
		<!--<div class="card-footer pt-0">
			Footer
		</div>-->
	</div>
	<!---->
	<div class="modal fade" id="courseModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
		aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Course Setting</h4>
				</div>
				<div class="modal-body">
					<div class="row">
						<div class="col-md-12">
							<form id="courseForm">
								<input type="hidden" class="form-control" value="" id="courseId" name="courseId">
								<div class="mb-3">
									<label class="form-label">The-Coach</label>
									<select class="form-select" value="" id="courseCoachId" name="courseCoachId">
										<option th:each="coach,coachStat:${coachs}" th:value="${coach.coachId}" th:text="${coach.coachName}"></option>
									</select>
								</div>
								<div class="mb-3">
									<label class="form-label">Title</label>
									<input type="text" class="form-control" value="" id="courseTitle" name="courseTitle">
								</div>
								<!-- Save button -->
								<div class="d-flex justify-content-end mt-4">
									<button type="button" class="btn btn-default"
										onclick="closeCourseModal()">Close</button>
									<button type="submit" class="btn btn-primary">Submit</button>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div><!-- /.modal-content -->
		</div><!-- /.modal -->
	</div>
	<script layout:fragment="script">
		var courseFormValidate = null
		$(document).ready(function () {
			courseFormValidate = 
			$("#courseForm").validate({
				rules: {
					courseId: {
						required: false
					},
					courseCoachId: {
						required: true
					},
					courseTitle: {
						required: true
					},
				},
				submitHandler: function (form) {
					var formData = $(form).serialize();
					saveCourse(formData);
					return false
				}
			})
			loadCourseTable()
		});
		function loadCourseTable() {
			$('#courseTable').bootstrapTable('destroy');
			$('#courseTable').bootstrapTable({
				url: "[[@{'/admin/courses'}]]",
				uniqueId: 'courseId',
				dataType: "json",
				pagination: true,
				pageSize: 10,
				queryParams: function (params) {
					var temp = {
						size: params.limit,
						page: (params.offset / params.limit),
						sort: params.sort,
						sortOrder: params.order
					};
					return temp;
				},
				sidePagination: "server",
				pageList: [30, 50],
				columns: [{
					title: 'Sort',
					formatter: function (value, row, index) {
						var pageSize = $('#courseTable').bootstrapTable('getOptions').pageSize;
						var pageNumber = $('#courseTable').bootstrapTable('getOptions').pageNumber;
						return pageSize * (pageNumber - 1) + index + 1;
					}
				}, {
					field: 'courseCoachId',
					title: 'The-Coach',
					formatter: function (value, row, index) {
						return row.coach.coachName
					}
				}, {
					field: 'courseTitle',
					title: 'Title',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					field: 'courseTime',
					title: 'Created',
					formatter: function (value, row, index) {
						return value
					}
				}, {
					title: 'Operate',
					field: 'courseId',
					class: 'text-center',
					width: '20%',
					formatter: function (value, row, index) {
						let res = '<button class="btn btn-sm btn-warning" onclick="modifyCourseModal(\'' + value + '\')">Modify</button>&nbsp;';
						res += '<button class="btn btn-sm btn-danger " onclick="removeCourse(\'' + value + '\')">Remove</button>';
						return res;
					}
				},],
				onPostBody: function (value) {
					$(".fixed-table-container").css('padding-bottom', '37px');
				}
			});
		}
		function closeCourseModal() {
			courseFormValidate.resetForm()
			$('#courseModal').modal('hide')
		}
		function modifyCourseModal(courseId) {
			if (courseId) {
				var data = $("#courseTable").bootstrapTable('getRowByUniqueId', courseId);
				$('#courseId').val(data.courseId)
				$('#courseCoachId').val(data.courseCoachId)
				$('#courseTitle').val(data.courseTitle)
			} else {
				$('#courseId').val('')
				$('#courseCoachId').val('')
				$('#courseTitle').val('')
			}
			$('#courseModal').modal('show')
		}
		function saveCourse(data) {
			$.post(ctx + "/admin/saveCourse", data, function (resp) {
				if (resp.status == 0) {
					loadCourseTable();
					$('#courseModal').modal('hide');
				}
				layer.msg(resp.msg);
			})
		}
		function removeCourse(courseId) {
			layer.msg("Do you want to delete it", {
				title: 'tip', icon: 3, btn: ["Confirm", "Cancel"],
				yes: function () {
					$.ajax({
						type: "get",
						url: ctx + "/admin/delCourse",
						data: {
							"courseId": courseId,
						},
						success: function (resp) {
							if (resp.status == 0) {
								loadCourseTable()
							}
							layer.msg(resp.msg);
						}
					});
				},
				btn2: function () {
					// layer.msg("Cancel");
				}
			});
		}
	</script>
</body>

</html>